package com.app.controller;

import com.app.enums.StatusCodeEnum;
import com.app.model.JiuquanUserModel;
import com.app.model.SpecialityManageModel;
import com.app.model.TrainModel;
import com.app.model.TrainPeopleModel;
import com.app.service.SpecialityManageService;
import com.app.service.TrainPeopleService;
import com.app.service.TrainService;
import com.app.service.UserService;
import com.app.util.CommonHelp;
import com.app.util.Result;
import com.app.util.TokenUtils;
import com.app.vo.TrainPeopleVo;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;

//培训学员报名信息
@RestController
@Controller
@RequestMapping("/jiuquan/trainPeople")
public class TrainPeopleController {
    @Autowired
    private TrainPeopleService trainPeopleService;
    @Autowired
    private UserService userService;

    @Autowired
    private TrainService trainService;

    @Autowired
    private SpecialityManageService specialityManageService;
    //查询列表
    @GetMapping("/getTrainPeopleList")
    public Result getTrainPeopleList(@RequestParam(defaultValue = "0") Integer page,
                                     @RequestParam(defaultValue = "10") Integer limit, String keyword) {
        TrainPeopleVo trainPeopleList = trainPeopleService.getTrainPeopleList(page, limit);
        return Result.success(trainPeopleList);
    }

    //详情
    @GetMapping("/getTrainPeopleById")
    public Result getTrainPeopleById(Integer id) {
        TrainPeopleModel trainPeople = trainPeopleService.getById(id);
        Integer specialityId = trainPeople.getSpecialityId();
        SpecialityManageModel speciality = specialityManageService.getById(specialityId);
        trainPeople.setSpecialityText(speciality.getName());
        //培训机构ID转汉字
        Integer trainId = trainPeople.getTrainId();
        TrainModel train = trainService.getById(trainId);
        trainPeople.setTrainText(train.getTrainInstitution());
        //培训类型转汉字
        if (trainPeople.getTrainType() == 1) {
            trainPeople.setTrainTypeText("创业培训");
        }
        if (trainPeople.getTrainType() == 2) {
            trainPeople.setTrainTypeText("技能培训");
        }
        trainPeople.setOpenId(null);
        return Result.success(trainPeople);
    }

    //报名列表
    @GetMapping("/getTrainPeopleByTrain")
    public Result getTrainPeopleByTrain(Integer page,Integer limit,Integer trainId) {
        TrainPeopleVo trainPeopleList =trainPeopleService.getTrainPeopleByTrain(page,limit,trainId);
        return Result.success(trainPeopleList);
    }

    //新增和修改
    @PostMapping("/addAndUpdateTrainPeople")
    public Result addAndUpdateTrainPeople(@RequestBody TrainPeopleModel trainPeopleModel) {
        TrainPeopleModel trainPeople = trainPeopleService.getById(trainPeopleModel.getId());
        if (trainPeople != null) {
            trainPeopleModel.setUpdateTime(CommonHelp.getNowTimestamp());
            trainPeopleService.updateById(trainPeopleModel);
            return Result.success("修改成功");
        } else {
            trainPeopleModel.setCreateTime(CommonHelp.getNowTimestamp());
            trainPeopleService.save(trainPeopleModel);
            return Result.success("添加成功");
        }
    }

    //删除
    @GetMapping("/delTrainPeople")
    public Result delTrainPeople(Integer id) {
        trainPeopleService.removeById(id);
        return Result.success("删除成功");
    }

    //用户报名新增
    @PostMapping("/userAddTrainPeople")
    public Result userAddTrainPeople(@RequestBody TrainPeopleModel trainPeopleModel, HttpServletRequest request) {
        String token = request.getHeader("token");
        if (token == null) {
            return Result.error(405, "token不存在");
        }
        String openId = TokenUtils.verify(token);
        JiuquanUserModel user = userService.getUserByOpenid(openId);
        if (user == null) {
            return Result.error(StatusCodeEnum.STATUS406.getCode(), StatusCodeEnum.STATUS406.getMsg());
        }
        TrainPeopleModel trainPeople = trainPeopleService.getById(trainPeopleModel.getId());
        if (trainPeople != null) {
            trainPeopleModel.setUpdateTime(CommonHelp.getNowTimestamp());
            trainPeopleService.updateById(trainPeopleModel);
            return Result.success("修改成功");
        } else {
            trainPeopleModel.setOpenId(openId);
            trainPeopleModel.setCreateTime(CommonHelp.getNowTimestamp());
            trainPeopleService.save(trainPeopleModel);
            return Result.success("添加成功");
        }
    }

    //查询用户报名培训列表
    @GetMapping("/getUserTrainList")
    public Result getUserTrainList(Integer page,Integer limit,HttpServletRequest request) {
        String token = request.getHeader("token");
        if (token == null) {
            return Result.error(405, "token不存在");
        }
        String openId = TokenUtils.verify(token);
        JiuquanUserModel user = userService.getUserByOpenid(openId);
        if (user == null) {
            return Result.error(406, "用户不存在");
        }
        TrainPeopleVo trainPeopleList = trainPeopleService.getUserTrainList(page, limit,openId);
        return Result.success(trainPeopleList);
    }

    //查询用户报名详情
    @GetMapping("/getUserTrainById")
    public Result getUserTrainById(Integer id,HttpServletRequest request) {
        String token = request.getHeader("token");
        if (token == null) {
            return Result.error(405, "token不存在");
        }
        String openId = TokenUtils.verify(token);
        JiuquanUserModel user = userService.getUserByOpenid(openId);
        if (user == null) {
            return Result.error(406, "用户不存在");
        }
        TrainPeopleModel trainPeople = trainPeopleService.getUserTrainById(id);
        Integer specialityId = trainPeople.getSpecialityId();
        SpecialityManageModel speciality = specialityManageService.getById(specialityId);
        trainPeople.setSpecialityText(speciality.getName());
        //培训机构ID转汉字
        Integer trainId = trainPeople.getTrainId();
        TrainModel train = trainService.getById(trainId);
        trainPeople.setTrainText(train.getTrainInstitution());
        //培训类型转汉字
        if (trainPeople.getTrainType() == 1) {
            trainPeople.setTrainTypeText("创业培训");
        }
        if (trainPeople.getTrainType() == 2) {
            trainPeople.setTrainTypeText("技能培训");
        }
        trainPeople.setOpenId(null);
        return Result.success(trainPeople);
    }
}
